package com.kumi.controller;


import com.alibaba.fastjson.JSON;
import com.kumi.controller.BaseController;
import com.kumi.exception.ApiException;
import com.kumi.util.DigestUtil;
import com.kumi.viewmodel.ApiResult;
import com.kumi.viewmodel.MapResult;
import com.kumi.viewmodel.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import static com.kumi.db.getConnection.getConnection;

/**
 * Created by Administrator on 2016/8/8 0008.
 */
@RestController
public class RegisterConterller extends BaseController {

    @RequestMapping(value = "registercheck")
    public ApiResult login(String phonenumber) {
        List<User> userList = select();
        String a="2";
        for (User user : userList) {
            if (phonenumber.equals(user.getPhonenumber())) {
               a="1";
                break;
            }
            else {
                a="2";
            }
        }

        MapResult mapResult = new MapResult();
       // mapResult.put("token", DigestUtil.Encrypt("0:"));
        mapResult.put("a",a);
        return mapResult;
    }



    public static List<User> select(){

        String sql = "SELECT phonenumber FROM myusers";
        List<User> userList = new ArrayList<User>();
        PreparedStatement preparedStatement;

        try {
            preparedStatement = getConnection().prepareStatement(sql);
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()){
                User loginUser = new User();
                loginUser.setPhonenumber(resultSet.getString("phonenumber"));
                userList.add(loginUser);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }

        return userList;

    }




    @RequestMapping(value = "register")
    public ApiResult login(String username, String password, String phonenumber) {
        User user = new User(username, password, phonenumber);
        insert(user);
        MapResult mapResult = new MapResult();
        //mapResult.put("token", DigestUtil.Encrypt("0:" + username));
        mapResult.put("aa",1);
        return mapResult;
    }


       public static void insert(User user) {
        //先得到数据库连接
        Connection connection = getConnection();

        //sql
        String sql = "INSERT INTO myusers(username,password,phonenumber) VALUES (?,?,?)";
        PreparedStatement preparedStatement = null;

        try {

            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, user.getUsername());
            preparedStatement.setString(2, user.getPassword());
            preparedStatement.setString(3, user.getPhonenumber());
            preparedStatement.execute();
            System.out.printf("insert successfully");
            preparedStatement.close();
            connection.close();
            //return a="1";
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}



